package com.tjcd.project.library_base.common.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatButton;


/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/15
 * @Description 短信倒计时
 * @SpittingGrooves
 */
public class CountDownView extends AppCompatButton {
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 倒计时的时长（毫秒）
     */
    private int milliSecond;
    /**
     * 按钮的text
     */
    private String btnText;
    /**
     * 倒计时时显示的文本
     */
    private String runningText;
    /**
     * 倒计时器
     */
    private CountDownTimer countDownTimer;
    /**
     * 倒计时停止txt
     */
    private String btnTextFinish;
    /**
     * 背景图
     */
    private Drawable bgDrawable;
    /**
     * 倒计时的默认时长
     */
    public static final int MILLI_SECOND = 60 * 1000;
    /**
     * 步长，1000ms
     */
    public static final int INTERVAL = 1000;

    public CountDownView(Context context) {
        this(context, null);
    }

    public CountDownView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        //首先将时长设置为默认值
        milliSecond = MILLI_SECOND;
        //读取按钮的text
        btnText = getText().toString();
        //设置倒计时显示的默认值
        runningText = btnText;
        //设置倒计时结束时显示的文本
        btnTextFinish = "重新发送";
        //获取背景图
        bgDrawable = getBackground();
    }

    /**
     * 开始倒计时
     */
    public void startCountDown(int milliSec, String txt) {
        //首先判断时长是否有效
        if (milliSec > 0) {
            milliSecond = milliSec;
        }
        //设置状态
        setEnabled(false);
        if (null != mCountDownListenr) {
            mCountDownListenr.onCountDownStart();
        }
        //然后判断显示的文字是否有
        try {
            runningText = txt;
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        //修正计时起点
        milliSecond = milliSecond + 100;
        //创建计时器，重写其方法，并启动
        countDownTimer = new CountDownTimer(milliSecond, INTERVAL) {
            @Override
            public void onTick(long l) {
                setText(l / 1000 + runningText);
            }

            @Override
            public void onFinish() {
                setEnabled(true);
                setText(btnTextFinish);
                countDownStop();
                if (null != mCountDownListenr) {
                    mCountDownListenr.onCountDownFinish();
                }
            }
        }.start();

    }

    /**
     * 停止倒计时
     */
    public void countDownStop() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        setEnabled(true);
    }

    /**
     * 离开界面时停止计时
     */
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        countDownStop();
    }

    /**
     * 回调监听
     */
    CountDownListener mCountDownListenr;

    /**
     * 设置回调
     *
     * @param countDownListener
     */
    public void setCountDownListener(CountDownListener countDownListener) {
        this.mCountDownListenr = countDownListener;
    }


    /**
     * 回调接口
     */
    public interface CountDownListener {
        void onCountDownStart();

        void onCountDownFinish();
    }
}
